package mh.log;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;

/**
 * Class used to create a properly styled and formatted html log file.<br>
 * This class divide each log record in parts and put them into a table<br>
 * +----+-------+-----------+--------------+---------+<br>
 * | ID | LEVEL | TIMESTAMP | CALLER_CLASS | MESSAGE |<br>
 * +----+-------+-----------+--------------+---------+<br>
 * 
 * @author Gianmarco
 * 
 */
public class MHFormatter extends Formatter {

	// This method is called for every log records
	@Override
	public String format(LogRecord rec) {
		StringBuffer buf = new StringBuffer(1000);
		// Bold any levels >= WARNING
		String tr = "";
		if (rec.getLevel().intValue() >= Level.WARNING.intValue()) {
			tr = "\t\t<tr class=\"red\">";
		} else if (rec.getLevel().intValue() == Level.INFO.intValue()) {
			tr = "\t\t<tr class=\"yellow\">";
		} else if (rec.getLevel().intValue() == Level.FINE.intValue()) {
			tr = "\t\t<tr class=\"green\">";
		} else {
			tr = "\t\t<tr>";
		}
		buf.append(tr);
		// buf.append("\t<tr>\n");

		buf.append("\t\t<td>");

		buf.append(rec.getSequenceNumber());

		buf.append("</td>\n");
		buf.append("\t\t<td>");

		if (rec.getLevel().intValue() >= Level.WARNING.intValue()) {
			buf.append("<b>");
			buf.append(rec.getLevel());
			buf.append("</b>");
		} else {
			buf.append(rec.getLevel());
		}
		buf.append("</td>\n");
		buf.append("\t\t<td>");

		buf.append(this.calcDate(rec.getMillis()));

		buf.append("</td>\n");
		buf.append("\t\t<td>");

		buf.append(rec.getLoggerName());

		buf.append("</td>\n");
		buf.append("\t\t<td>");

		buf.append(this.formatMessage(rec));

		buf.append("</td>\n");
		buf.append("\t</tr>\n");
		return buf.toString();
	}

	private String calcDate(long millisecs) {
		SimpleDateFormat date_format = new SimpleDateFormat("[ HH:mm:ss ]");
		Date resultdate = new Date(millisecs);
		return date_format.format(resultdate);
	}

	// This method is called just after the handler using this
	// formatter is created
	@Override
	public String getHead(Handler h) {
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy -  HH:mm:ss");
		return "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
				+ "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">\n<head>\n" + "<title>" + dateFormat.format(new Date())
				+ "</title>\n<link rel=\"stylesheet\" type=\"text/css\" href=\"../style.css\" /></head>\n<body>\n<pre>\n<table class=\"table\">\n" + "\t<tr>\n"
				+ "\t\t<th>No.</th>\n" + "\t\t<th>Level</th>\n" + "\t\t<th>Time</th>\n" + "\t\t<th>Class</th>\n" + "\t\t<th>Log Message</th>\n" + "\t</tr>\n";

	}

	// This method is called just after the handler using this
	// formatter is closed
	@Override
	public String getTail(Handler h) {
		return "</table>\n  </PRE></BODY>\n</HTML>\n";
	}
}